package edu.gatech.volt2.drdoommgt.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextPane;

import edu.gatech.volt2.drdoommgt.domain.Labwork;
import edu.gatech.volt2.drdoommgt.domain.Name;
import edu.gatech.volt2.drdoommgt.domain.TreatmentRecord;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

public class InvoiceView extends JFrame implements IInvoiceView{

	private JPanel contentPane;
	private JTextPane txtpntotal;
	private JTextPane txtpndate;
	private JLabel lblDate;
	private JTextPane txtpnname;
	private JTextPane txtpndoctor;
	private JTextPane servicesList;
	private JButton closeButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InvoiceView frame = new InvoiceView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public InvoiceView() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 515, 335);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		closeButton = new JButton("Close");
		panel.add(closeButton, BorderLayout.EAST);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		JLabel lblPatientName = new JLabel("Patient Name:");
		lblPatientName.setBounds(6, 6, 122, 16);
		panel_1.add(lblPatientName);
		
		JLabel lblAttendingDoctor = new JLabel("Attending Doctor:");
		lblAttendingDoctor.setBounds(6, 34, 122, 16);
		panel_1.add(lblAttendingDoctor);
		
		JLabel lblServicesProvided = new JLabel("Services Provided:");
		lblServicesProvided.setBounds(6, 62, 122, 16);
		panel_1.add(lblServicesProvided);
		
		servicesList = new JTextPane();
		servicesList.setBackground(Color.WHITE);
		servicesList.setText("[Services]");
		servicesList.setBounds(127, 62, 360, 122);
		servicesList.setAlignmentX(RIGHT_ALIGNMENT);
		panel_1.add(servicesList);
		
		txtpndoctor = new JTextPane();
		txtpndoctor.setText("[Doctor]");
		txtpndoctor.setBounds(127, 34, 360, 16);
		txtpndoctor.setAlignmentX(RIGHT_ALIGNMENT);
		panel_1.add(txtpndoctor);
		
		txtpnname = new JTextPane();
		txtpnname.setText("[Name]");
		txtpnname.setBounds(127, 6, 360, 16);
		txtpnname.setAlignmentX(RIGHT_ALIGNMENT);
		panel_1.add(txtpnname);
		
		lblDate = new JLabel("Date:");
		lblDate.setBounds(6, 193, 104, 16);
		panel_1.add(lblDate);
		
		txtpndate = new JTextPane();
		txtpndate.setText("[Date]");
		txtpndate.setBounds(127, 193, 360, 16);
		txtpndate.setAlignmentX(RIGHT_ALIGNMENT);
		panel_1.add(txtpndate);
		
		JLabel lblTotal = new JLabel("Total:");
		lblTotal.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblTotal.setBounds(6, 221, 61, 16);
		panel_1.add(lblTotal);
		
		txtpntotal = new JTextPane();
		txtpntotal.setText("[Total]");
		txtpntotal.setEnabled(false);
		txtpntotal.setEditable(false);
		txtpntotal.setBounds(127, 221, 360, 16);
		txtpntotal.setAlignmentX(RIGHT_ALIGNMENT);
		panel_1.add(txtpntotal);
	}

	@Override
	public JButton getCloseButton() {
		return closeButton;
	}

	@Override
	public void setPatientName(Name name) {
		this.txtpnname.setText(name.toString());
	}

	@Override
	public void setTreatmentRecord(TreatmentRecord tr) {
		SimpleDateFormat fmt = new SimpleDateFormat("MM/d/yyyy");
		this.txtpndate.setText(fmt.format(tr.getDate().getTime()));
		double total = 0.0;
		this.txtpndoctor.setText(tr.getTreatingDoc().getName().toString());
		for( Labwork lw : tr.getDocOrders().getLabwork()){
			total += lw.getLab().getPrice();
			String ctext = this.servicesList.getText();
			ctext += "["+lw.getLab().getType() + "] "+lw.getLab().getDescription()+"   @   $"+lw.getLab().getPrice()+ "\n";
			this.servicesList.setText(ctext);
			ctext = null;
		}
		this.txtpntotal.setText("$"+total);
	}

	@Override
	public void showSelf() {
		this.setVisible(true);
	}

	@Override
	public void addListener(ActionListener al) {
		this.closeButton.addActionListener(al);
	}

	@Override
	public void hideSelf() {
		this.setVisible(false);
	}
}
